//
//  connection.m
//  Discover
//
//  Created by GreatranLee on 27/10/14.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "connection.h"

@implementation connection
//+ (void)networkGetHandlerWith:(NSString *)str block:(void(^)(id result))block;
//{
//    //1.处理地址
//    NSString *strURL = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    NSURL *url = [NSURL URLWithString:strURL];
//
//    //2.创建请求
//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
//    request.HTTPMethod = @"get";
//
//    //3.链接服务器
//    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//
//        NSError *error = nil;
//        id temp = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
//        block(temp);
//
//    }];
//
//}

+ (void)networkPostHandlerWith:(NSString *)str bodyStr:(NSString *)bodyStr block:(void(^)(id result))block;
{
    //1.处理地址
    NSString *strURL = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strURL];
    
    //2.创建请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    request.HTTPMethod = @"post";
    NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPBody = body;
    
    //3.链接服务器
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSError *error = nil;
        id temp = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        block(temp);
        
    }];
}
+ (void)networkWithURL:(NSString *)urlStr completion:(void (^)(id))block
{
    //1.处理地址
    NSString *strURL = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strURL];
    
    //2.创建请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    request.HTTPMethod = @"get";
    
    //3.链接服务器
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSError *error = nil;
        id temp = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        
        //        将返回的结果，利用block传给外部使用的类
        block(temp);
    }];
    
}

@end
